<?php

namespace App\Http\Controllers\App;

use App\Http\Common\Constant;
use App\Http\Controllers\Controller;
use App\Models\BonusPool;
use App\Models\BonusPoolLog;
use App\Models\MemberBonusQueue;
use App\Models\MemberPayCountMoneyQueue;
use App\Models\Store;
use App\Models\StoreIncomeLog;

class MoneyCommonController extends Controller
{
    /**
     * 会员现金和余额支付会调用这个接口
     * 会员现金和余额支付返佣给商家和资金池
     * @param $store_id 店铺di
     * @param $money 会员支付金额
     * @param $member_order_id 订单id
     * @throws \Exception
     */
    public function memberPayCommission($store_id, $money, $member_order_id)
    {
        $store = Store::where('id', $store_id)->first();

        $inconme_money = floor($money * Constant::STORE_INCOME_PROPORTION * 100) / 100;

        // 店铺收益
        $store->total_money += $inconme_money;
        $store->usable_money += $inconme_money;
        $save_store = $store->save();
        if (!$save_store) {
            throw new \Exception("store save error");
        }

        // 店铺收益日志
        $store_income_log = new StoreIncomeLog();
        $store_income_log->store_id = $store_id;
        $store_income_log->member_order_id = $member_order_id;
        $store_income_log->money = $inconme_money;
        $store_income_log->proportion = Constant::STORE_INCOME_PROPORTION;
        $save = $store_income_log->save();
        if (!$save) {
            throw new \Exception("店铺收益日志写入失败");
        }

        // 资金池收益
        $bonus_pool = BonusPool::where('id', 1)->first();

        $bonus_pool_inconme_money = floor($money * Constant::BONUS_POOL_PROPORTION * 100) / 100;
        $bonus_pool->total_money += $bonus_pool_inconme_money;
        $bonus_pool->usable_money += $bonus_pool_inconme_money;
        $save = $bonus_pool->save();
        if (!$save) {
            throw new \Exception("资金池收益写入失败");
        }

        // 资金池日志
        $bonus_pool_log = new BonusPoolLog();
        $bonus_pool_log->store_id = $store_id;
        $bonus_pool_log->member_order_id = $member_order_id;
        $bonus_pool_log->money = $bonus_pool_inconme_money;
        $bonus_pool_log->proportion = Constant::BONUS_POOL_PROPORTION;
        $save = $bonus_pool_log->save();
        if (!$save) {
            throw new \Exception("资金池日志写入失败");
        }
    }

    /**
     * 会员现金和余额支付会调用这个接口
     * 会员消费总记录
     */
    public function memberBonusQueue($member_id)
    {
        $member_bonus_queue = new MemberBonusQueue();
        $member_bonus_queue->member_id = $member_id;
        $member_bonus_queue->status = 0;
        $member_bonus_queue->money = Constant::MEMBER_BONUS_MONEY;
        $save = $member_bonus_queue->save();
        if (!$save) {
            throw new \Exception("奖金池写入失败");
        }
    }

    /**
     * 会员现金和余额支付会调用这个接口
     * 消费进入队列
     */
    public function memberPayCountMoneyQueue($member_id, $money)
    {
        $member_pay_count_money_queue = MemberPayCountMoneyQueue::where('member_id', $member_id)->first();
        if ($member_pay_count_money_queue) {
            $member_pay_count_money_queue->member_id = $member_id;
            $member_pay_count_money_queue->total_money += $money;
            $save = $member_pay_count_money_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额增加失败");
            }
        } else {
            $member_pay_count_money_queue = new MemberPayCountMoneyQueue();
            $member_pay_count_money_queue->member_id = $member_id;
            $member_pay_count_money_queue->total_money = $member_pay_count_money_queue->total_money + $money;
            $save = $member_pay_count_money_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额新增失败");
            }
        }

        $total_queue = MemberPayCountMoneyQueue::where('member_id', $member_id)->first();

        // 累计消费到达多少进入奖金池
        if ($total_queue->total_money > Constant::MEMBER_TOTAL_PAY_MONEY) {
            $num = floor($total_queue->total_money / Constant::MEMBER_TOTAL_PAY_MONEY);
            for ($i = 0; $i < $num; $i++) {
                // 写入奖金池
                $this->memberBonusQueue($member_id);
            }
            //写入奖金池后再减去总消费金额
            $total_queue->total_money = $total_queue->total_money - Constant::MEMBER_TOTAL_PAY_MONEY * $num;
            $save = $total_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额写入失败");
            }
        }
    }


    public function test()
    {
        try {
            \DB::beginTransaction();
            //会员支付成功调用
            $this->memberPayCommission(6, 5999.11, 1);
            $this->memberPayCountMoneyQueue(1, 5999.11);
            \DB::commit();
            echo 'ok';
        } catch (\Exception $e) {
            \DB::rollBack();
            var_dump($e->getMessage());
        }
    }


}